home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gsht.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  21.1 KB  |  717 lines

  1. /* Copyright (C) 1989, 1996, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gsht.c,v 1.2 2000/09/19 19:00:29 lpd Exp $ */
  20. /* setscreen operator for Ghostscript library */
  21. #include "memory_.h"
  22. #include <stdlib.h>        /* for qsort */
  23. #include "gx.h"
  24. #include "gserrors.h"
  25. #include "gsstruct.h"
  26. #include "gsutil.h"        /* for gs_next_ids */
  27. #include "gxarith.h"        /* for igcd */
  28. #include "gzstate.h"
  29. #include "gxdevice.h"        /* for gzht.h */
  30. #include "gzht.h"
  31.  
  32. /* Forward declarations */
  33. void gx_set_effective_transfer(P1(gs_state *));
  34.  
  35. /* Structure types */
  36. public_st_ht_order();
  37. private_st_ht_order_component();
  38. public_st_ht_order_comp_element();
  39. public_st_halftone();
  40. public_st_device_halftone();
  41.  
  42. /* GC procedures */
  43.  
  44. private
  45. ENUM_PTRS_WITH(ht_order_enum_ptrs, gx_ht_order *porder) return 0;
  46. case 0: ENUM_RETURN((porder->data_memory ? porder->levels : 0));
  47. case 1: ENUM_RETURN((porder->data_memory ? porder->bit_data : 0));
  48. case 2: ENUM_RETURN(porder->cache);
  49. case 3: ENUM_RETURN(porder->transfer);
  50. ENUM_PTRS_END
  51. private
  52. RELOC_PTRS_WITH(ht_order_reloc_ptrs, gx_ht_order *porder)
  53. {
  54.     if (porder->data_memory) {
  55.     RELOC_VAR(porder->levels);
  56.     RELOC_VAR(porder->bit_data);
  57.     }
  58.     RELOC_VAR(porder->cache);
  59.     RELOC_VAR(porder->transfer);
  60. }
  61. RELOC_PTRS_END
  62.  
  63. private 
  64. ENUM_PTRS_WITH(halftone_enum_ptrs, gs_halftone *hptr) return 0;
  65. case 0:
  66. switch (hptr->type)
  67. {
  68.     case ht_type_spot:
  69.     ENUM_RETURN((hptr->params.spot.transfer == 0 ?
  70.              hptr->params.spot.transfer_closure.data :
  71.              0));
  72.     case ht_type_threshold:
  73.     ENUM_RETURN_CONST_STRING_PTR(gs_halftone, params.threshold.thresholds);
  74.     case ht_type_threshold2:
  75.     return ENUM_CONST_BYTESTRING(&hptr->params.threshold2.thresholds);
  76.     case ht_type_client_order:
  77.     ENUM_RETURN(hptr->params.client_order.client_data);
  78.     case ht_type_multiple:
  79.     case ht_type_multiple_colorscreen:
  80.     ENUM_RETURN(hptr->params.multiple.components);
  81.     case ht_type_none:
  82.     case ht_type_screen:
  83.     case ht_type_colorscreen:
  84.     return 0;
  85. }
  86. case 1:
  87. switch (hptr->type) {
  88.     case ht_type_threshold:
  89.     ENUM_RETURN((hptr->params.threshold.transfer == 0 ?
  90.              hptr->params.threshold.transfer_closure.data :
  91.              0));
  92.     case ht_type_threshold2:
  93.     ENUM_RETURN(hptr->params.threshold2.transfer_closure.data);
  94.     case ht_type_client_order:
  95.     ENUM_RETURN(hptr->params.client_order.transfer_closure.data);
  96.     default:
  97.     return 0;
  98. }
  99. ENUM_PTRS_END
  100.  
  101. private RELOC_PTRS_WITH(halftone_reloc_ptrs, gs_halftone *hptr)
  102. {
  103.     switch (hptr->type) {
  104.     case ht_type_spot:
  105.         if (hptr->params.spot.transfer == 0)
  106.         RELOC_PTR(gs_halftone, params.spot.transfer_closure.data);
  107.         break;
  108.     case ht_type_threshold:
  109.         RELOC_CONST_STRING_PTR(gs_halftone, params.threshold.thresholds);
  110.         if (hptr->params.threshold.transfer == 0)
  111.         RELOC_PTR(gs_halftone, params.threshold.transfer_closure.data);
  112.         break;
  113.     case ht_type_threshold2:
  114.         RELOC_CONST_BYTESTRING_VAR(hptr->params.threshold2.thresholds);
  115.         RELOC_OBJ_VAR(hptr->params.threshold2.transfer_closure.data);
  116.         break;
  117.     case ht_type_client_order:
  118.         RELOC_PTR(gs_halftone, params.client_order.client_data);
  119.         RELOC_PTR(gs_halftone, params.client_order.transfer_closure.data);
  120.         break;
  121.     case ht_type_multiple:
  122.     case ht_type_multiple_colorscreen:
  123.         RELOC_PTR(gs_halftone, params.multiple.components);
  124.         break;
  125.     case ht_type_none:
  126.     case ht_type_screen:
  127.     case ht_type_colorscreen:
  128.         break;
  129.     }
  130. }
  131. RELOC_PTRS_END
  132.  
  133. /* setscreen */
  134. int
  135. gs_setscreen(gs_state * pgs, gs_screen_halftone * phsp)
  136. {
  137.     gs_screen_enum senum;
  138.     int code = gx_ht_process_screen(&senum, pgs, phsp,
  139.                     gs_currentaccuratescreens());
  140.  
  141.     if (code < 0)
  142.     return code;
  143.     return gs_screen_install(&senum);
  144. }
  145.  
  146. /* currentscreen */
  147. int
  148. gs_currentscreen(const gs_state * pgs, gs_screen_halftone * phsp)
  149. {
  150.     switch (pgs->halftone->type) {
  151.     case ht_type_screen:
  152.         *phsp = pgs->halftone->params.screen;
  153.         return 0;
  154.     case ht_type_colorscreen:
  155.         *phsp = pgs->halftone->params.colorscreen.screens.colored.gray;
  156.         return 0;
  157.     default:
  158.         return_error(gs_error_undefined);
  159.     }
  160. }
  161.  
  162. /* .currentscreenlevels */
  163. int
  164. gs_currentscreenlevels(const gs_state * pgs)
  165. {
  166.     return pgs->dev_ht->order.num_levels;
  167. }
  168.  
  169. /* .setscreenphase */
  170. int
  171. gx_imager_setscreenphase(gs_imager_state * pis, int x, int y,
  172.              gs_color_select_t select)
  173. {
  174.     if (select == gs_color_select_all) {
  175.     int i;
  176.  
  177.     for (i = 0; i < gs_color_select_count; ++i)
  178.         gx_imager_setscreenphase(pis, x, y, (gs_color_select_t) i);
  179.     return 0;
  180.     } else if (select < 0 || select >= gs_color_select_count)
  181.     return_error(gs_error_rangecheck);
  182.     pis->screen_phase[select].x = x;
  183.     pis->screen_phase[select].y = y;
  184.     return 0;
  185. }
  186. int
  187. gs_setscreenphase(gs_state * pgs, int x, int y, gs_color_select_t select)
  188. {
  189.     int code = gx_imager_setscreenphase((gs_imager_state *) pgs, x, y,
  190.                     select);
  191.  
  192.     /*
  193.      * If we're only setting the source phase, we don't need to do
  194.      * unset_dev_color, because the source phase doesn't affect painting
  195.      * with the current color.
  196.      */
  197.     if (code >= 0 && (select == gs_color_select_texture ||
  198.               select == gs_color_select_all)
  199.     )
  200.     gx_unset_dev_color(pgs);
  201.     return code;
  202. }
  203.  
  204. /* .currentscreenphase */
  205. int
  206. gs_currentscreenphase(const gs_state * pgs, gs_int_point * pphase,
  207.               gs_color_select_t select)
  208. {
  209.     if (select < 0 || select >= gs_color_select_count)
  210.     return_error(gs_error_rangecheck);
  211.     *pphase = pgs->screen_phase[select];
  212.     return 0;
  213. }
  214.  
  215. /* currenthalftone */
  216. int
  217. gs_currenthalftone(gs_state * pgs, gs_halftone * pht)
  218. {
  219.     *pht = *pgs->halftone;
  220.     return 0;
  221. }
  222.  
  223. /* ------ Internal routines ------ */
  224.  
  225. /* Process one screen plane. */
  226. int
  227. gx_ht_process_screen_memory(gs_screen_enum * penum, gs_state * pgs,
  228.         gs_screen_halftone * phsp, bool accurate, gs_memory_t * mem)
  229. {
  230.     gs_point pt;
  231.     int code = gs_screen_init_memory(penum, pgs, phsp, accurate, mem);
  232.  
  233.     if (code < 0)
  234.     return code;
  235.     while ((code = gs_screen_currentpoint(penum, &pt)) == 0)
  236.     if ((code = gs_screen_next(penum, (*phsp->spot_function) (pt.x, pt.y))) < 0)
  237.         return code;
  238.     return 0;
  239. }
  240.  
  241. /*
  242.  * Internal procedure to allocate and initialize either an internally
  243.  * generated or a client-defined halftone order.  For spot halftones,
  244.  * the client is responsible for calling gx_compute_cell_values.
  245.  */
  246. int
  247. gx_ht_alloc_ht_order(gx_ht_order * porder, uint width, uint height,
  248.              uint num_levels, uint num_bits, uint strip_shift,
  249.              const gx_ht_order_procs_t *procs, gs_memory_t * mem)
  250. {
  251.     porder->width = width;
  252.     porder->height = height;
  253.     porder->raster = bitmap_raster(width);
  254.     porder->shift = strip_shift;
  255.     porder->orig_height = porder->height;
  256.     porder->orig_shift = porder->shift;
  257.     porder->full_height = ht_order_full_height(porder);
  258.     porder->num_levels = num_levels;
  259.     porder->num_bits = num_bits;
  260.     porder->procs = procs;
  261.     porder->data_memory = mem;
  262.     porder->levels =
  263.     (uint *)gs_alloc_byte_array(mem, porder->num_levels, sizeof(uint),
  264.                     "alloc_ht_order_data(levels)");
  265.     porder->bit_data =
  266.     gs_alloc_byte_array(mem, porder->num_bits,
  267.                 porder->procs->bit_data_elt_size,
  268.                 "alloc_ht_order_data(bit_data)");
  269.     if (porder->levels == 0 || porder->bit_data == 0) {
  270.     gs_free_object(mem, porder->bit_data, "alloc_ht_order_data(bit_data)");
  271.     porder->bit_data = 0;
  272.     gs_free_object(mem, porder->levels, "alloc_ht_order_data(levels)");
  273.     porder->levels = 0;
  274.     return_error(gs_error_VMerror);
  275.     }
  276.     porder->cache = 0;
  277.     porder->transfer = 0;
  278.     return 0;
  279. }
  280.  
  281. /* Allocate and initialize the contents of a halftone order. */
  282. /* The client must have set the defining values in porder->params. */
  283. int
  284. gx_ht_alloc_order(gx_ht_order * porder, uint width, uint height,
  285.           uint strip_shift, uint num_levels, gs_memory_t * mem)
  286. {
  287.     gx_ht_order order;
  288.     int code;
  289.  
  290.     order = *porder;
  291.     gx_compute_cell_values(&order.params);
  292.     code = gx_ht_alloc_ht_order(&order, width, height, num_levels,
  293.                 width * height, strip_shift,
  294.                 &ht_order_procs_default, mem);
  295.     if (code < 0)
  296.     return code;
  297.     *porder = order;
  298.     return 0;
  299. }
  300.  
  301. /*
  302.  * Allocate and initialize a threshold order, which may use the short
  303.  * representation.
  304.  */
  305. int
  306. gx_ht_alloc_threshold_order(gx_ht_order * porder, uint width, uint height,
  307.                 uint num_levels, gs_memory_t * mem)
  308. {
  309.     gx_ht_order order;
  310.     uint num_bits = width * height;
  311.     const gx_ht_order_procs_t *procs =
  312.     (num_bits > 2000 && num_bits <= max_ushort ?
  313.      &ht_order_procs_short : &ht_order_procs_default);
  314.     int code;
  315.  
  316.     order = *porder;
  317.     gx_compute_cell_values(&order.params);
  318.     code = gx_ht_alloc_ht_order(&order, width, height, num_levels,
  319.                 width * height, 0, procs, mem);
  320.     if (code < 0)
  321.     return code;
  322.     *porder = order;
  323.     return 0;
  324. }
  325.  
  326. /* Allocate and initialize the contents of a client-defined halftone order. */
  327. int
  328. gx_ht_alloc_client_order(gx_ht_order * porder, uint width, uint height,
  329.              uint num_levels, uint num_bits, gs_memory_t * mem)
  330. {
  331.     gx_ht_order order;
  332.     int code;
  333.  
  334.     order = *porder;
  335.     order.params.M = width, order.params.N = 0;
  336.     order.params.R = 1;
  337.     order.params.M1 = height, order.params.N1 = 0;
  338.     order.params.R1 = 1;
  339.     gx_compute_cell_values(&order.params);
  340.     code = gx_ht_alloc_ht_order(&order, width, height, num_levels,
  341.                 num_bits, 0, &ht_order_procs_default, mem);
  342.     if (code < 0)
  343.     return code;
  344.     *porder = order;
  345.     return 0;
  346. }
  347.  
  348. /* Compare keys ("masks", actually sample values) for qsort. */
  349. private int
  350. compare_samples(const void *p1, const void *p2)
  351. {
  352.     ht_sample_t m1 = ((const gx_ht_bit *)p1)->mask;
  353.     ht_sample_t m2 = ((const gx_ht_bit *)p2)->mask;
  354.  
  355.     return (m1 < m2 ? -1 : m1 > m2 ? 1 : 0);
  356. }
  357. /* Sort the halftone order by sample value. */
  358. void
  359. gx_sort_ht_order(gx_ht_bit * recs, uint N)
  360. {
  361.     int i;
  362.  
  363.     /* Tag each sample with its index, for sorting. */
  364.     for (i = 0; i < N; i++)
  365.     recs[i].offset = i;
  366.     qsort((void *)recs, N, sizeof(*recs), compare_samples);
  367. #ifdef DEBUG
  368.     if (gs_debug_c('H')) {
  369.     uint i;
  370.  
  371.     dlputs("[H]Sorted samples:\n");
  372.     for (i = 0; i < N; i++)
  373.         dlprintf3("%5u: %5u: %u\n",
  374.               i, recs[i].offset, recs[i].mask);
  375.     }
  376. #endif
  377. }
  378.  
  379. /*
  380.  * Construct the halftone order from a sampled spot function.  Only width x
  381.  * strip samples have been filled in; we must replicate the resulting sorted
  382.  * order vertically, shifting it by shift each time.  See gxdht.h regarding
  383.  * the invariants that must be restored.
  384.  */
  385. void
  386. gx_ht_construct_spot_order(gx_ht_order * porder)
  387. {
  388.     uint width = porder->width;
  389.     uint num_levels = porder->num_levels;    /* = width x strip */
  390.     uint strip = num_levels / width;
  391.     gx_ht_bit *bits = (gx_ht_bit *)porder->bit_data;
  392.     uint *levels = porder->levels;
  393.     uint shift = porder->orig_shift;
  394.     uint full_height = porder->full_height;
  395.     uint num_bits = porder->num_bits;
  396.     uint copies = num_bits / (width * strip);
  397.     gx_ht_bit *bp = bits + num_bits - 1;
  398.     uint i;
  399.  
  400.     gx_sort_ht_order(bits, num_levels);
  401.     if_debug5('h',
  402.           "[h]spot order: num_levels=%u w=%u h=%u strip=%u shift=%u\n",
  403.           num_levels, width, porder->orig_height, strip, shift);
  404.     /* Fill in the levels array, replicating the bits vertically */
  405.     /* if needed. */
  406.     for (i = num_levels; i > 0;) {
  407.     uint offset = bits[--i].offset;
  408.     uint x = offset % width;
  409.     uint hy = offset - x;
  410.     uint k;
  411.  
  412.     levels[i] = i * copies;
  413.     for (k = 0; k < copies;
  414.          k++, bp--, hy += num_levels, x = (x + width - shift) % width
  415.         )
  416.         bp->offset = hy + x;
  417.     }
  418.     /* If we have a complete halftone, restore the invariant. */
  419.     if (num_bits == width * full_height) {
  420.     porder->height = full_height;
  421.     porder->shift = 0;
  422.     }
  423.     gx_ht_construct_bits(porder);
  424. }
  425.  
  426. /* Construct a single offset/mask. */
  427. void
  428. gx_ht_construct_bit(gx_ht_bit * bit, int width, int bit_num)
  429. {
  430.     uint padding = bitmap_raster(width) * 8 - width;
  431.     int pix = bit_num;
  432.     ht_mask_t mask;
  433.     byte *pb;
  434.  
  435.     pix += pix / width * padding;
  436.     bit->offset = (pix >> 3) & -size_of(mask);
  437.     mask = (ht_mask_t) 1 << (~pix & (ht_mask_bits - 1));
  438.     /* Replicate the mask bits. */
  439.     pix = ht_mask_bits - width;
  440.     while ((pix -= width) >= 0)
  441.     mask |= mask >> width;
  442.     /* Store the mask, reversing bytes if necessary. */
  443.     bit->mask = 0;
  444.     for (pb = (byte *) & bit->mask + (sizeof(mask) - 1);
  445.      mask != 0;
  446.      mask >>= 8, pb--
  447.     )
  448.     *pb = (byte) mask;
  449. }
  450.  
  451. /* Construct offset/masks from the whitening order. */
  452. /* porder->bits[i].offset contains the index of the bit position */
  453. /* that is i'th in the whitening order. */
  454. void
  455. gx_ht_construct_bits(gx_ht_order * porder)
  456. {
  457.     uint i;
  458.     gx_ht_bit *phb;
  459.  
  460.     for (i = 0, phb = (gx_ht_bit *)porder->bit_data;
  461.      i < porder->num_bits;
  462.      i++, phb++)
  463.     gx_ht_construct_bit(phb, porder->width, phb->offset);
  464. #ifdef DEBUG
  465.     if (gs_debug_c('H')) {
  466.     dlprintf1("[H]Halftone order bits 0x%lx:\n", (ulong)porder->bit_data);
  467.     for (i = 0, phb = (gx_ht_bit *)porder->bit_data;
  468.          i < porder->num_bits;
  469.          i++, phb++)
  470.         dlprintf3("%4d: %u:0x%lx\n", i, phb->offset,
  471.               (ulong) phb->mask);
  472.     }
  473. #endif
  474. }
  475.  
  476. /* Release a gx_device_halftone by freeing its components. */
  477. /* (Don't free the gx_device_halftone itself.) */
  478. void
  479. gx_ht_order_release(gx_ht_order * porder, gs_memory_t * mem, bool free_cache)
  480. {
  481.     if (free_cache && porder->cache)
  482.     gx_ht_free_cache(mem, porder->cache);
  483.     gs_free_object(mem, porder->transfer, "gx_ht_order_release(transfer)");
  484.     if (porder->data_memory) {
  485.     gs_free_object(porder->data_memory, porder->bit_data,
  486.                "gx_ht_order_release(bit_data)");
  487.     gs_free_object(porder->data_memory, porder->levels,
  488.                "gx_ht_order_release(levels)");
  489.     }
  490. }
  491. void
  492. gx_device_halftone_release(gx_device_halftone * pdht, gs_memory_t * mem)
  493. {
  494.     if (pdht->components) {
  495.     int i;
  496.  
  497.     /* One of the components might be the same as the default */
  498.     /* order, so check that we don't free it twice. */
  499.     for (i = 0; i < pdht->num_comp; ++i)
  500.         if (pdht->components[i].corder.bit_data !=
  501.         pdht->order.bit_data
  502.         ) {        /* Currently, all orders except the default one */
  503.         /* own their caches. */
  504.         gx_ht_order_release(&pdht->components[i].corder, mem, true);
  505.         }
  506.     gs_free_object(mem, pdht->components,
  507.                "gx_dev_ht_release(components)");
  508.     pdht->components = 0;
  509.     pdht->num_comp = 0;
  510.     }
  511.     gx_ht_order_release(&pdht->order, mem, false);
  512. }
  513.  
  514. /*
  515.  * Install a device halftone in an imager state.  Note that this does not
  516.  * read or update the client halftone.  There is a special check for pdht ==
  517.  * pis->dev_ht, for the benefit of the band rendering code.
  518.  */
  519. int
  520. gx_imager_dev_ht_install(gs_imager_state * pis,
  521.              const gx_device_halftone * pdht,
  522.              gs_halftone_type type, const gx_device * dev)
  523. {
  524.     gx_device_halftone *pgdht = pis->dev_ht;
  525.  
  526.     if ((ulong) pdht->order.raster *
  527.     (pdht->order.num_bits / pdht->order.width) > pis->ht_cache->bits_size
  528.     )
  529.     return_error(gs_error_limitcheck);
  530.     if (pdht == pgdht)
  531.     DO_NOTHING;        /* special hack for band renderer */
  532.     else if (pgdht != 0 && pgdht->rc.ref_count == 1 &&
  533.     pgdht->rc.memory == pdht->rc.memory
  534.     ) {
  535.     /* The current device halftone isn't shared. */
  536.     /* Just release its components. */
  537.     gx_device_halftone_release(pgdht, pgdht->rc.memory);
  538.     } else {            /* The device halftone is shared or not yet allocated. */
  539.     rc_unshare_struct(pis->dev_ht, gx_device_halftone,
  540.               &st_device_halftone, pdht->rc.memory,
  541.               return_error(gs_error_VMerror),
  542.               "gx_imager_dev_ht_install");
  543.     pgdht = pis->dev_ht;
  544.     }
  545.     {
  546.     rc_header rc;
  547.  
  548.     rc = pgdht->rc;
  549.     *pgdht = *pdht;
  550.     pgdht->rc = rc;
  551.     }
  552.     pgdht->id = gs_next_ids(1);
  553.     pgdht->type = type;
  554.     /* Clear the cache, to avoid confusion in case the address of */
  555.     /* a new order vector matches that of a (deallocated) old one. */
  556.     gx_ht_clear_cache(pis->ht_cache);
  557.     /* Set the color_indices according to the device color_info. */
  558.     /* Also compute the LCM of the primary color cell sizes. */
  559.     /* Note that for strip halftones, the "cell size" is the */
  560.     /* theoretical fully expanded size with shift = 0. */
  561.     if (pdht->components != 0) {
  562.     static const gs_ht_separation_name dcnames[5][4] =
  563.     {
  564.         {gs_ht_separation_Default},        /* not used */
  565.         {gs_ht_separation_Default, gs_ht_separation_Default,
  566.          gs_ht_separation_Default, gs_ht_separation_Gray
  567.         },
  568.         {gs_ht_separation_Default},        /* not used */
  569.         {gs_ht_separation_Red, gs_ht_separation_Green,
  570.          gs_ht_separation_Blue, gs_ht_separation_Default
  571.         },
  572.         {gs_ht_separation_Cyan, gs_ht_separation_Magenta,
  573.          gs_ht_separation_Yellow, gs_ht_separation_Black
  574.         }
  575.     };
  576.     static const gs_ht_separation_name cscnames[4] =
  577.     {gs_ht_separation_Red, gs_ht_separation_Green,
  578.      gs_ht_separation_Blue, gs_ht_separation_Default
  579.     };
  580.     int num_comps = dev->color_info.num_components;
  581.     const gs_ht_separation_name *cnames = dcnames[num_comps];
  582.     int lcm_width = 1, lcm_height = 1;
  583.     uint i;
  584.  
  585.     /* Halftones set by setcolorscreen, and (we think) */
  586.     /* Type 2 and Type 4 halftones, are supposed to work */
  587.     /* for both RGB and CMYK, so we need a special check here. */
  588.     if (num_comps == 4 &&
  589.         (type == ht_type_colorscreen ||
  590.          type == ht_type_multiple_colorscreen)
  591.         )
  592.         cnames = cscnames;
  593.     if_debug4('h', "[h]dcnames=%lu,%lu,%lu,%lu\n",
  594.           (ulong) cnames[0], (ulong) cnames[1],
  595.           (ulong) cnames[2], (ulong) cnames[3]);
  596.     memset(pgdht->color_indices, 0, sizeof(pdht->color_indices));
  597.     for (i = 0; i < pdht->num_comp; i++) {
  598.         const gx_ht_order_component *pcomp =
  599.         &pdht->components[i];
  600.         int j;
  601.  
  602.         if_debug2('h', "[h]cname[%d]=%lu\n",
  603.               i, (ulong) pcomp->cname);
  604.         for (j = 0; j < 4; j++) {
  605.         if (pcomp->cname == cnames[j]) {
  606.             if_debug2('h', "[h]color_indices[%d]=%d\n",
  607.                   j, i);
  608.             pgdht->color_indices[j] = i;
  609.         }
  610.         }
  611.     }
  612.     /* Now do a second pass to compute the LCM. */
  613.     /* We have to do it this way in case some entry in */
  614.     /* color_indices is still 0. */
  615.     for (i = 0; i < 4; ++i) {
  616.         const gx_ht_order_component *pcomp =
  617.         &pdht->components[pgdht->color_indices[i]];
  618.         uint cw = pcomp->corder.width;
  619.         uint ch = pcomp->corder.full_height;
  620.         int dw = lcm_width / igcd(lcm_width, cw);
  621.         int dh = lcm_height / igcd(lcm_height, ch);
  622.  
  623.         lcm_width = (cw > max_int / dw ? max_int : cw * dw);
  624.         lcm_height = (ch > max_int / dh ? max_int : ch * dh);
  625.     }
  626.     pgdht->lcm_width = lcm_width;
  627.     pgdht->lcm_height = lcm_height;
  628.     } else {            /* Only one component. */
  629.     pgdht->lcm_width = pgdht->order.width;
  630.     pgdht->lcm_height = pgdht->order.full_height;
  631.     }
  632.     if_debug2('h', "[h]LCM=(%d,%d)\n",
  633.           pgdht->lcm_width, pgdht->lcm_height);
  634.     gx_imager_set_effective_xfer(pis);
  635.     return 0;
  636. }
  637.  
  638. /*
  639.  * Install a new halftone in the graphics state.  Note that we copy the top
  640.  * level of the gs_halftone and the gx_device_halftone, and take ownership
  641.  * of any substructures.
  642.  */
  643. int
  644. gx_ht_install(gs_state * pgs, const gs_halftone * pht,
  645.           const gx_device_halftone * pdht)
  646. {
  647.     gs_memory_t *mem = pht->rc.memory;
  648.     gs_halftone *old_ht = pgs->halftone;
  649.     gs_halftone *new_ht;
  650.     int code;
  651.  
  652.     if (old_ht != 0 && old_ht->rc.memory == mem &&
  653.     old_ht->rc.ref_count == 1
  654.     )
  655.     new_ht = old_ht;
  656.     else
  657.     rc_alloc_struct_1(new_ht, gs_halftone, &st_halftone,
  658.               mem, return_error(gs_error_VMerror),
  659.               "gx_ht_install(new halftone)");
  660.     code = gx_imager_dev_ht_install((gs_imager_state *) pgs,
  661.                  pdht, pht->type, gs_currentdevice_inline(pgs));
  662.     if (code < 0) {
  663.     if (new_ht != old_ht)
  664.         gs_free_object(mem, new_ht, "gx_ht_install(new halftone)");
  665.     return code;
  666.     }
  667.     if (new_ht != old_ht)
  668.     rc_decrement(old_ht, "gx_ht_install(old halftone)");
  669.     {
  670.     rc_header rc;
  671.  
  672.     rc = new_ht->rc;
  673.     *new_ht = *pht;
  674.     new_ht->rc = rc;
  675.     }
  676.     pgs->halftone = new_ht;
  677.     gx_unset_dev_color(pgs);
  678.     return 0;
  679. }
  680.  
  681. /* Reestablish the effective transfer functions, taking into account */
  682. /* any overrides from halftone dictionaries. */
  683. void
  684. gx_imager_set_effective_xfer(gs_imager_state * pis)
  685. {
  686.     const gx_device_halftone *pdht = pis->dev_ht;
  687.  
  688.     pis->effective_transfer = pis->set_transfer;    /* default */
  689.     if (pdht == 0)
  690.     return;            /* not initialized yet */
  691.     if (pdht->components == 0) {    /* Check for transfer function override in single halftone */
  692.     gx_transfer_map *pmap = pdht->order.transfer;
  693.  
  694.     if (pmap != 0)
  695.         pis->effective_transfer.indexed[0] =
  696.         pis->effective_transfer.indexed[1] =
  697.         pis->effective_transfer.indexed[2] =
  698.         pis->effective_transfer.indexed[3] = pmap;
  699.     } else {            /* Check in all 4 standard separations */
  700.     int i;
  701.  
  702.     for (i = 0; i < 4; ++i) {
  703.         gx_transfer_map *pmap =
  704.         pdht->components[pdht->color_indices[i]].corder.
  705.         transfer;
  706.  
  707.         if (pmap != 0)
  708.         pis->effective_transfer.indexed[i] = pmap;
  709.     }
  710.     }
  711. }
  712. void
  713. gx_set_effective_transfer(gs_state * pgs)
  714. {
  715.     gx_imager_set_effective_xfer((gs_imager_state *) pgs);
  716. }
  717.